package com.sprites;

import com.bricks2break.Statics;

import android.graphics.Paint;
import sheep.collision.CollisionListener;
import sheep.game.Sprite;

public class Brick extends Sprite implements CollisionListener {
	private int width;
	private int height;
	private Paint paint;
	private float startX = 0, startY = 0, endX = 0, endY = 0;
	private int value;

	/**
	 * Brick class constructor
	 * 
	 * @param paint
	 *            The color of the brick
	 * @param value
	 *            The value of the brick(Number of points a player gets to destroy
	 *            the brick)
	 */
	public Brick(Paint paint, int value) {
		super(Statics.image_brick);
		height = (int) Statics.image_brick.getHeight();
		width = (int) Statics.image_brick.getWidth();
		this.paint = paint;
		this.value = value;
		addCollisionListener(this);
		setGroup((int) Statics.COLLISION_GROUP);
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public int getValue() {
		return value;
	}

	/**
	 * Update method
	 */
	public void update(float dt) {
		super.update(dt);
	}

	/**
	 * Draw method
	 */
	public void draw(android.graphics.Canvas canvas) {
		super.draw(canvas);
		canvas.drawRect(startX, startY, endX, endY, paint);
	}

	/**
	 * Method that removes a brick
	 * 
	 * @return
	 */
	public int destroy() {
		removeCollisionListener(this);
		Statics.brickGroup.removeBrick(this);
		return 0;
	}

	/**
	 * Method that checks collisions
	 */
	public void collided(Sprite a, Sprite b) {
		if (b instanceof Ball) {
			if (((Ball) b).getCurrentOwner() != null) {
				((Ball) b).getCurrentOwner().addPointsToScore(value);
			}
			destroy();
		}
	}

	/**
	 * Method that set the position of a brick and updates variables that is
	 * used to draw the brick
	 * 
	 * @param x
	 *            the x coordinate of the new position
	 * @param y
	 *            the y coordinate of the new position
	 */
	public void setPositionAndUpdateVariable(float x, float y) {
		setPosition(x, y);
		startX = x - width / 2 + 2;
		endX = x + width / 2 - 2;
		startY = y - height / 2 + 2;
		endY = y + height / 2 - 2;
	}
}